package com.daocao.hua.util;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.daocao.hua.R;

import java.io.File;
import java.util.concurrent.ExecutionException;


/**
 * Created by WIN7 on 2016/11/11.
 */
public class GlideUtils {
    public static void loadImage(Context context, ImageView imageView, String url) {

        Glide
                .with(context)

                .load(url)
                .skipMemoryCache(true)
                .thumbnail(0.2f)//缩略比例
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(imageView);
    }

    public static void loadImageRound(Context context, final ImageView imageView, String url) {

        Glide
                .with(context)
                .load(url)
                .thumbnail(0.2f)//缩略比例
                //.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存
                .placeholder(R.color.image_default)
                //.transform(new GlideRoundTransform(context))
                // .crossFade(1000) // 淡入加载效果 可设置时长，默认“300ms”
                .into(imageView);/*new SimpleTarget<GlideDrawable>() {//解决第一次加载时只显示占位图（有圆形图片时）
                    @Override
                    public void onResourceReady(GlideDrawable resource,
                                                GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });*/
    }

    public static void loadLargeImage(Context context, final ImageView imageView, String url) {

        Glide
                .with(context)
                .load(url)
                .thumbnail(0.2f)//缩略比例
                //.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存
              /*  .placeholder(R.mipmap.message_icon)*/
                .error(R.mipmap.message_icon)
                //.transform(new GlideRoundTransform(context))
                // .crossFade(1000) // 淡入加载效果 可设置时长，默认“300ms”
                .into(imageView);/*new SimpleTarget<GlideDrawable>() {//解决第一次加载时只显示占位图（有圆形图片时）
                    @Override
                    public void onResourceReady(GlideDrawable resource,
                                                GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });*/


    }

    public static void downloadImage(Context context, String url) {

        try {
            File ss = Glide.with(context.getApplicationContext())
                    .load(url)
                    .downloadOnly(750, 1334)
                    .get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


    }

    public static void preload(Context context, String url) {
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.SOURCE).preload();
    }

    public static void preloadintoImage(Context context, String url, ImageView view) {
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(view);
    }
}